// 这份代码专门处理逻辑层的操作
const sickenDao = require('../dao/sickenDao');
const fileDao = require('../dao/fileDao');

const powerService = require('./powerService');

const sickenService = {
    /*
     * 新增病例档案接口
     */
    async addSickenRecord(uid, studentId, time, hospital, sickenTypeId, note, photoUrl) {
        let result;
        // 用户权限认证
        try {
            result = await powerService.checkPowerByUid(uid, '新增病例档案');
            if (result == false) {
                return {success: false, msg: "权限不足，无法操作"};
            }
        } catch (err) {
            return {success: false, msg: "操作过程中出错"};
        }
        // 真正的执行逻辑
        try {
            result = await sickenDao.addSickenRecord(studentId, time, hospital, sickenTypeId, note, photoUrl);
            result = { success: true, msg: "新增成功", insertId: result.insertId };
        } catch (error) {
            result = { success: false, msg: "修改数据库时出错，修改失败" };
        }
        return result;
    },
    async addSickenRecordNoImg(uid, studentId, time, hospital, sickenTypeId, note) {
        let result;
        // 用户权限认证
        try {
            result = await powerService.checkPowerByUid(uid, '新增病例档案');
            if (result == false) {
                return {success: false, msg: "权限不足，无法操作"};
            }
        } catch (err) {
            return {success: false, msg: "操作过程中出错"};
        }
        // 真正的执行逻辑
        try {
            result = await sickenDao.addSickenRecordNoImg(studentId, time, hospital, sickenTypeId, note);
            result = { success: true, msg: "新增成功", insertId: result.insertId };
        } catch (error) {
            result = { success: false, msg: "修改数据库时出错，修改失败" };
        }
        return result;
    },
    async getAllSickenType() {
        let result;
        try {
            result = await sickenDao.getAllSickenType();
            result = { success: true, msg: "查询成功", allSickenType: result };
        } catch (error) {
            result = { success: false, msg: "查询数据库时出错，查询失败" };
        }
        return result;
    },
    /*
     * 查询某个学生的所有病例档案接口
     */
    async getSickenRecordByStudent(uid, userType, studentId) {
        // 用户权限认证
        let result;
        try {
            result = await powerService.checkPowerByUid(uid, '查看学生信息');
            if (result == false) {
                return {success: false, msg: "权限不足，无法操作"};
            }
        } catch (err) {
            return {success: false, msg: "操作过程中出错"};
        }
        // 真正的执行逻辑
        try {
            let insertResult = await sickenDao.getSickenRecordByStudent(uid, studentId);
            for (let i = 0; i < insertResult.length; i++) {
                let imgId = insertResult[i].imgId;
                let photoUrl = "";
                if (imgId) {
                    let tmp = await fileDao.downloadPublicFile(imgId);
                    if (tmp.success) {
                        photoUrl = tmp.url;
                    }
                }
                insertResult[i].photoUrl = photoUrl;
            }
            result = { success: true, msg: "查询成功", result: insertResult };
        } catch (error) {
            result = { success: false, msg: "操作数据库时出错，查询失败" };
        }
        return result;
    }
};

module.exports = sickenService;